package com.study.spring.ch03.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/ch03") //①处理器的通用映射前缀
public class HelloWorldController {

	@RequestMapping(value = "/hello") //②相对于①处的映射进行窄化
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","ch03-hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * 普通URL路径映射
	 * @RequestMapping(value={"/action01", "/normal/action02"})：多个URL路径可以映射
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/action01", "/normal/action02"})
	public ModelAndView handleNormalRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","normal-url: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * URL路径模板映射
	 * @RequestMapping(value="/urlTemplate/{param}")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/{param01}","/urlMapping/{param02}/create",
			"/urlMapping/{param03}/create/{param04}"})
	public ModelAndView handleUrlTemplateRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","urlTemplate: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Ant风格的URL路径映射
	 * @RequestMapping(value="/antTemplate/**")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/**"})
	public ModelAndView handleAntTemplate01Request(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","antTemplate,**: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Ant风格的URL路径映射  "/product?"
	 * @RequestMapping(value="/antTemplate/product?")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/product?"})
	public ModelAndView handleAntTemplate02Request(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","antTemplate,?: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Ant风格的URL路径映射  "/product*"
	 * @RequestMapping(value="/antTemplate/product*")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/product*"})
	public ModelAndView handleAntTemplate03Request(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","antTemplate,*: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Ant风格的URL路径映射  "/product/*"
	 * @RequestMapping(value="/antTemplate/product*")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/product/*"})
	public ModelAndView handleAntTemplate04Request(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","antTemplate,/*: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Ant风格的URL路径映射  "/product/*"
	 * @RequestMapping(value="/antTemplate/product*")
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value={"/urlMapping/product/**/{productId}"})
	public ModelAndView handleAntTemplate05Request(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","antTemplate,/**/{productId}: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
	
	/**
	 * Reg风格的URL路径映射  "/products/*"
	 * @RequestMapping(value="/products/{categoryCode:\\d+}-{pageNumber:\\d+}")
	 * /products/123-1
	 * @param arg0
	 * @param arg1
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/products/{categoryCode:\\d+}-{pageNumber:\\d+}")
	public ModelAndView handleRegTemplateRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("message","regTemplate: hello!");
		mv.setViewName("ch03/hello");
		return mv;
	}
}
